package com.bdzl.ecs.util.electronicUtils;


import com.alibaba.fastjson.JSONObject;

import com.fadada.utils.FddClient;
import com.fadada.sdk.util.crypt.FddEncryptTool;
import com.fadada.sdk.util.http.HttpsUtil;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * @创建人 zhaochunyu
 * @创建时间 2021年4月27日17:06:02
 * @描述 定位系统
 */
public class FddClientBaseUtils extends FddClient {
    public FddClientBaseUtils(String appId, String secret, String version, String url) {
        super(appId, secret, version, url);
    }

    //修改个人信息
    public String invokeInfoChangeAuto(String customer_id, String email, String id_mobile) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String msgDigest;
            // Base64(SHA1(appid+md5(timestamp)+SHA1(appsecret)))
            String sha1 = FddEncryptTool.sha1(super.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(super.getSecret()));
            msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));

            params.add(new BasicNameValuePair("customer_id", customer_id));
            params.add(new BasicNameValuePair("email", email));
            params.add(new BasicNameValuePair("id_mobile", id_mobile));

            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return HttpsUtil.doCaPost(super.getURLOfInfoChange(), params);
    }


    /**
     * 删除章引接口
     * @param customer_id 客户id
     * @param signature_id 签章编号
     * @param
     * @return
     */
    public String invokeRemoveSignature(String customer_id,String signature_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {

            String timeStamp = HttpsUtil.getTimeStamp();
            String msgDigest;
            // Base64(SHA1(appid+md5(timestamp)+SHA1(appsecret)))
            String sha1 = FddEncryptTool.sha1(super.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(super.getSecret()+customer_id+signature_id));
            msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));

            params.add(new BasicNameValuePair("customer_id", customer_id));
            params.add(new BasicNameValuePair("signature_id", signature_id));

            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return HttpsUtil.doCaPost(super.getURLOfRemoveSignature(), params);
    }

    /**
     * 设置默认章接口
     * @param customer_id  客户id
     * @param signature_id  盖章编号
     * @param
     * @return
     */
    public String invokeDefaultSignature(String customer_id, String signature_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String msgDigest;
            // Base64(SHA1(appid+md5(timestamp)+SHA1(appsecret)))
            String sha1 = FddEncryptTool.sha1(super.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(super.getSecret()+customer_id+signature_id));
            msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));

            params.add(new BasicNameValuePair("customer_id", customer_id));
            params.add(new BasicNameValuePair("signature_id", signature_id));

            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        String json= JSONObject.toJSONString(params);
        System.out.println(json);
        System.out.println(super.getURLOfDefaultSignature());
        return HttpsUtil.doCaPost(super.getURLOfDefaultSignature(), params);
    }

/*
    public  String addSignature(String customer_id, String signature_img_base64) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String msgDigest;
            String sha1 = FddEncryptTool.sha1(super.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(super.getSecret()+customer_id+signature_img_base64));
            msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            params.add(new BasicNameValuePair("customer_id", customer_id));
            params.add(new BasicNameValuePair("signature_img_base64",signature_img_base64));
            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return HttpsUtil.doCaPost(super.getURLOfAddSignature(), params);
    }
*/



    /**
     *  新建章接口
     * @param customer_id 客户id
     * @param signature_img_base64 印章图片
     * @return
     */
    public  String addSignature(String customer_id, String signature_img_base64) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String msgDigest;
            String sha1 = FddEncryptTool.sha1(super.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(super.getSecret()+customer_id+signature_img_base64));
            msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            params.add(new BasicNameValuePair("customer_id", customer_id));
            params.add(new BasicNameValuePair("signature_img_base64",signature_img_base64));
            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return HttpsUtil.doCaPost(super.getURLOfAddSignature(), params);
    }


    public  String querySignature(String customer_id, String signature_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String msgDigest;
            String sha1 = FddEncryptTool.sha1(super.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(super.getSecret()+customer_id+signature_id));
            msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            params.add(new BasicNameValuePair("customer_id", customer_id));
            params.add(new BasicNameValuePair("signature_id",signature_id));
            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return HttpsUtil.doCaPost(super.getURLOfQuerySignature(), params);
    }

    public  String replaceSignature(String customer_id,String signature_id ,String signature_img_base64) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String msgDigest;
            String sha1 = FddEncryptTool.sha1(super.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(super.getSecret()+customer_id+signature_id+signature_img_base64));
            msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            params.add(new BasicNameValuePair("customer_id", customer_id));
            params.add(new BasicNameValuePair("signature_img_base64",signature_img_base64));
            params.add(new BasicNameValuePair("signature_id",signature_id));
            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return HttpsUtil.doCaPost(super.getURLOfReplaceSignature(), params);
    }



    public String pushdocExtsign(String validity,String return_url,String limit_type,String push_type,String transaction_id, String customer_id, String client_role, String contract_id, String doc_title, String sign_keyword, String keyword_strategy, String notify_url) {
        ArrayList params = new ArrayList();
        try {
            String timestamp = HttpsUtil.getTimeStamp();
            String sha1 = FddEncryptTool.sha1(super.getAppId() + FddEncryptTool.md5Digest(timestamp) + FddEncryptTool.sha1(super.getSecret() + contract_id+transaction_id+push_type+customer_id+sign_keyword));
            String msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            params.add(new BasicNameValuePair("transaction_id", transaction_id));
            params.add(new BasicNameValuePair("customer_id", customer_id));
            params.add(new BasicNameValuePair("client_role", client_role));
            params.add(new BasicNameValuePair("contract_id", contract_id));
            params.add(new BasicNameValuePair("doc_title", doc_title));
            params.add(new BasicNameValuePair("position_type", "0"));
            params.add(new BasicNameValuePair("sign_keyword", sign_keyword));
            params.add(new BasicNameValuePair("keyword_strategy", keyword_strategy));
            params.add(new BasicNameValuePair("notify_url", notify_url));
            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
            params.add(new BasicNameValuePair("push_type", push_type));
            params.add(new BasicNameValuePair("limit_type", limit_type));
            params.add(new BasicNameValuePair("return_url", return_url));
            params.add(new BasicNameValuePair("validity", validity));
            params.add(new BasicNameValuePair("timestamp", timestamp));
        } catch (Exception var13) {
            var13.printStackTrace();
            throw new RuntimeException(var13);
        }

        return HttpsUtil.doPost(super.getPushdocExtsign(), params);
    }
}
